import { MindGraph } from "./MindGraph";
import { LayoutOptions, Bound, Direction } from './models'
import { Node } from './Node'

export class LayoutProvider {
    mindGraph: MindGraph = null
    // 配置项
    opts: LayoutOptions = null
    // 是否是 side 模式 
    isside: boolean = true
    // 边界 
    bounds: Bound = null
    // TODO:
    cacheValid: boolean = false
    constructor(mindGraph: MindGraph, options: LayoutOptions) {
        this.mindGraph = mindGraph
        this.opts = options
        this.isside = this.opts.mode == 'side'
        this.bounds = null
        this.cacheValid = false
    }

    /**
     * 布局初始化
     */
    init() {
        console.debug('layout.init')
    }

    /**
     * 布局重置
     */
    reset() {
        console.debug('layout.reset');
        this.bounds = {n:0,s:0,w:0,e:0};
    }

    /**
     * 切换 node 子节点的展开与收缩
     * @param node 
     */
    toggleNode(node: Node) {
        if(node.isroot){
            return;
        }
        if(node.expanded){
            this.collapseNode(node);
        }else{
            this.expandNode(node);
        }
    }

    /**
     * Node 对象是否可见
     */
    isVisible (node: Node) {
        var layout_data = node._data.layout;
        if(('visible' in layout_data) && !layout_data.visible){
            return false;
        }else{
            return true;
        } 
    }

    /**
     * 展开 node 的子节点
     * @param node 
     */
    expandNode (node: Node) {
        node.expanded = true;
        this.partLayout(node);
        this.setVisible(node.children,true);
    }
    
    /**
     * 收缩 node 的所有子节点
     * @param node 
     */
    collapseNode (node: Node) {
        node.expanded = false;
        this.partLayout(node);
        this.setVisible(node.children,false);
    }

    /**
     * 设置节点数组的可见性
     * @param nodes 
     * @param visible 
     */
    setVisible(nodes: Node[], visible: boolean) {
        var i = nodes.length;
        var node = null;
        var layout_data = null;
        while(i--){
            node = nodes[i];
            layout_data = node._data.layout;
            if(node.expanded){
                this.setVisible(node.children,visible);
            }else{
                this.setVisible(node.children,false);
            }
            if(!node.isroot){
                node._data.layout.visible = visible;
            }
        }
    }

    /**
     * 局部布局
     * @param node 
     */
    partLayout (node: Node) {
        var root = this.mindGraph.mind.root;
        if(!!root){
            var root_layout_data = root._data.layout;
            if(node.isroot){ // 如果 node 是根节点，则左右都要布局
                root_layout_data.outer_height_right=this._layout_offset_subnodes_height(root_layout_data.right_nodes);
                root_layout_data.outer_height_left=this._layout_offset_subnodes_height(root_layout_data.left_nodes);
            }else{ // 如果 node 不是根节点，则只布局对应方向的节点
                if(node._data.layout.direction == Direction.Right){
                    root_layout_data.outer_height_right=this._layout_offset_subnodes_height(root_layout_data.right_nodes);
                }else{
                    root_layout_data.outer_height_left=this._layout_offset_subnodes_height(root_layout_data.left_nodes);
                }
            }
            this.bounds.s = Math.max(root_layout_data.outer_height_left,root_layout_data.outer_height_right);
            this.cacheValid = false;
        }else{
            console.warn('can not found root node');
        }
    }

    private _layout_offset_subnodes_height(nodes: Node[]) {

    }
    
}